package cn.ecut.servlet;

import cn.ecut.model.Content;
import cn.ecut.model.User;
import cn.ecut.services.ContentServices;
import cn.ecut.services.UserServices;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/contentPublish")
public class ContentPublishServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String content = request.getParameter("content");
        HttpSession session = request.getSession();
        Object u = session.getAttribute("user");

        User user = (User) u;

        Content c = new Content() ;
        c.setContent( content );
        c.setUser( user );

        ContentServices cs = new ContentServices() ;
        boolean result = cs.addContent( c ) ;
        String msg = "" ;
        if( result ){
            // 发布成功，回到主页
            msg = "发布成功" ;
            request.setAttribute("msg" , msg );
            request.getRequestDispatcher("/index.jsp").forward( request , response );
        }else{
            msg = "发布失败" ;
            request.setAttribute("msg" , msg );
            request.getRequestDispatcher("/index.jsp").forward( request , response );
        }
    }
}
